package com.bayleaf.web.numeric;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

import com.bayleaf.common.NumericUtil;
import com.bayleaf.core.BayleafServlet;
import com.bayleaf.core.Model;

@WebServlet(name="numeric",
            urlPatterns={"/numeric"},
            initParams={ @WebInitParam(name="title", value="NumericConverter"), @WebInitParam(name="jsp", value="/numeric/NumericConverter") } )
public class NumericServlet extends BayleafServlet {

	public String process(Model model) throws Exception {
		NumericUtil nu = new NumericUtil();
	
		System.out.println(model.getString("input"));
		int inputValue = Integer.parseInt(model.getString("input"));
		byte[] bBytes = nu.int2byte(inputValue);
		String sBinary = "";
		String sDecimal = "";
		// convBinary
		
    	model.setString("decimal", model.getString("input"));
    	model.setString("convBinary", nu.convBinary(inputValue));
    	model.setString("convOctal", nu.convOctal(inputValue));
    	model.setString("convHeximal", nu.convHeximal(inputValue));
		for (int i = 0; i < 4; i++)		{
			sBinary += nu.viewBinaryFormat(bBytes)[i] + " ";
		}		
    	model.setString("viewBinaryFormat", sBinary);
		
    	
    	for (int i = 0; i < 4; i++)		{
    		sDecimal += nu.viewDecimalFormat(bBytes)[i] + " ";
		}    	
    	model.setString("viewDecimalFormat", sDecimal);
        
    	return null;
	}
	

}
